package com.example.oscarchen.myapplication;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.oscarchen.myapplication.viewmodel.ChronometerViewModel;
import com.example.oscarchen.myapplication.viewmodel.LiveDataTimerViewModel;
import com.example.oscarchen.myapplication.viewmodel.SeekBarViewModel;

/*
 * Copyright Withive, Inc. and/or its affiliates.
 * Created by Oscar.Chen on 11/16/2018
 * Email:  chanruida@outlook.com
 */
public class StartFragment extends Fragment {

    private SeekBar mSeekBar;
    private TextView mTextView;

    private SeekBarViewModel mSeekBarViewModel;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_5, container, false);

        mSeekBar = root.findViewById(R.id.seekBar);
        mTextView = root.findViewById(R.id.textView);

        mSeekBarViewModel = ViewModelProviders.of(getActivity()).get(SeekBarViewModel.class);

        subscribeSeekBar();

        return root;
    }

    private void subscribeSeekBar() {
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    mSeekBarViewModel.seekbarValue.setValue(progress);

                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mSeekBarViewModel.seekbarValue.observe(getActivity(), new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer value) {
                if (value != null) {
                    mSeekBar.setProgress(value);
                    mTextView.setText("" + value);
                }
            }
        });

    }
}